//
//  LspNbNewNounView.swift
//  LspSwiftFinayTest
//
//  Created by lsp's mac pro on 2018/5/2.
//  Copyright © 2018年 lsp's mac pro. All rights reserved.
//

import UIKit

class LspNbNewNounView: UIView {

    @IBOutlet weak var enterButton: UIButton!
    @IBOutlet weak var scrollview: UIScrollView!
    
    @IBOutlet weak var pageControl: UIPageControl!
    
    @IBAction func enterClick() {
        self.removeFromSuperview()
    }
    
   
    class func nounView() -> LspNbNewNounView {
        let nib = UINib(nibName: "LspNbNewNounView", bundle: nil)
        let v = nib.instantiate(withOwner: nil, options: nil)[0]
        return v as! LspNbNewNounView
    }
    
    override func awakeFromNib() {
        let count = 4
        let mainScreen = UIScreen.main.bounds
        for i in 0..<count {
            let imageNamed = "new_feature_\(i+1)"
            
            print(imageNamed)
            
            let imageSc = UIImageView(image: UIImage(named: imageNamed))
            imageSc.frame = mainScreen.offsetBy(dx: CGFloat(i)*mainScreen.width, dy: 0)
            print(mainScreen.width)
            scrollview.addSubview(imageSc)
            print(imageSc)
        }
        
        scrollview.contentSize = CGSize(width: mainScreen.width * CGFloat(count+1), height: mainScreen.height)
        scrollview.bounces=false
        scrollview.showsVerticalScrollIndicator=false
        scrollview.showsHorizontalScrollIndicator=false
        scrollview.isPagingEnabled=true
        scrollview.delegate=self
        enterButton.isHidden=true
    }
}
extension LspNbNewNounView: UIScrollViewDelegate{
    func scrollViewDidEndDecelerating(_ scrollView: UIScrollView) {
        //计算滚动到哪一屏
        let page = Int(scrollview.contentOffset.x/scrollview.bounds.width)
        if page == scrollView.subviews.count {
            self.removeFromSuperview()
        }
        print(page)
        if page == scrollView.subviews.count-1 {
            enterButton.isHidden=false
        }else{
            enterButton.isHidden=true
        }
    }
    
  
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        //运动中的时候让enterbutton先隐藏
        enterButton.isHidden=true
        
        //加0.5是让它在这个代理方法里面成为整数
        let  page = Int(scrollView.contentOffset.x/scrollView.bounds.width)
        
        pageControl.currentPage=page
        
        if page == scrollView.subviews.count {
            pageControl.isHidden=true
        }
        
        
        
    }
    
}
